#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QDir>
#include <QFileSystemWatcher>
#include <QTextStream>
#include <QDataStream>
#include <QMessageBox>
#include <QIODevice>
#include <QFileDialog>
#include <QString>
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);




}

MainWindow::~MainWindow()
{
    delete ui;
}

/**
 *
 * 文本文件写入到磁盘
 * @brief MainWindow::on_pushButton_clicked
 */
void MainWindow::on_pushButton_clicked()
{
    QString content = ui->plainTextEdit->toPlainText();
    if(!content.isEmpty())
    {
        QString path = ui->lineEdit->text();
        if(!path.isEmpty())
        {
            QFile file(path);
            if(file.open(QFile::WriteOnly | QFile::Truncate))
            {
                QTextStream out(&file);
                out << content << Qt::endl;
                qDebug() << content << Qt::endl;
                file.close();
                QMessageBox::information(this,"title","写入文件");
            }
        }
    }
    else
    {
        QMessageBox::information(this,"title","输入内容");
    }
}

/**
 * 读取文件
 * @brief MainWindow::on_pushButton_2_clicked
 */
void MainWindow::on_pushButton_2_clicked()
{
    QFile file(ui->lineEdit->text());
    if(file.open(QFile::ReadOnly))
    {
        // QTextStream in(&file);
        // QString content = in.readAll();
        // qDebug() << content << Qt::endl;


        QByteArray array;
        while(!file.atEnd())
        {
            array+=file.readLine();
        }

        ui->plainTextEdit->appendPlainText(array);
        file.close();
    }
}


void MainWindow::on_pushButton_3_clicked()
{
    QString path = QFileDialog::getOpenFileName(this,"打开文件","d:\\");
    if(!path.isEmpty())
    {
        ui->lineEdit->setText(path);
    }
}

